<template>
  <div class="comments-container">
    <!-- 顶部导航栏 -->
    <header class="comments-header">
      <i class="fa fa-arrow-left back-btn" @click="$router.back()"></i>
      <h1>我的评论</h1>
      <div class="header-placeholder"></div>
    </header>

    <!-- 加载中 -->
    <div class="loading-container" v-if="isLoading">
      <div class="loading-spinner">
        <i class="fa fa-spinner fa-spin"></i>
      </div>
      <p>加载中...</p>
    </div>

    <!-- 有评论 -->
    <main class="comments-main" v-else-if="orderArr.length">
      <div class="comments-count">
        <i class="fa fa-comment"></i>
        <span>共 {{ orderArr.length }} 条评论</span>
      </div>

      <ul class="comments-list">
        <li v-for="order in orderArr" :key="order.id" class="comment-item">
          <div class="comment-header">
            <div class="shop-info">
              <img :src="order.business?.businessImg || '/default-shop.png'" class="shop-image" />
              <div class="shop-details">
                <h3 class="shop-title">{{ order.business?.businessName || '未知商家' }}</h3>
                <p class="comment-date">{{ formatTime(order.updateTime.split('.')[0]) }}</p>
              </div>
            </div>
            <div class="rating-display">
              <span class="rating-text">{{ order.star }}分</span>
              <div class="rating-stars">
                <i class="fa fa-star" v-for="n in 5" :key="n" :class="{ rated: n <= order.star }" />
              </div>
            </div>
          </div>

          <div class="comment-content">
            <p class="comment-text">{{ order.comment || '暂无评论内容' }}</p>
          </div>

          <div class="comment-actions">
            <button class="action-btn modify-btn" @click="openEditModal(order)">
              <i class="fa fa-edit"></i>
              <span>编辑</span>
            </button>
            <button class="action-btn remove-btn" @click="deleteComment(order)">
              <i class="fa fa-trash"></i>
              <span>删除</span>
            </button>
          </div>
        </li>
      </ul>
    </main>

    <!-- 空状态 -->
    <div class="no-comments" v-else>
      <div class="empty-state">
        <div class="empty-icon">
          <i class="fa fa-comment-o"></i>
        </div>
        <h3>暂无评论记录</h3>
        <p>您还没有发表过任何评论</p>
        <button class="browse-shops-btn" @click="$router.push('/index')">
          <i class="fa fa-store"></i>
          去逛逛商家
        </button>
      </div>
    </div>

    <!-- 编辑评论模态框 -->
    <div class="edit-modal-overlay" v-if="showEditModal" @click="closeEditModal">
      <div class="edit-modal-content" @click.stop>
        <div class="modal-header">
          <h2>编辑评论</h2>
          <button class="close-modal-btn" @click="closeEditModal">
            <i class="fa fa-times"></i>
          </button>
        </div>
        <div class="modal-body">
          <div class="modal-shop-info">
            <img :src="editingOrder.business?.businessImg || '/default-shop.png'" class="modal-shop-image" />
            <div class="modal-shop-details">
              <h3>{{ editingOrder.business?.businessName || '未知商家' }}</h3>
              <p>{{ formatTime(editingOrder.orderDate) }}</p>
            </div>
          </div>

          <div class="rating-section">
            <label>评分：</label>
            <div class="modal-rating-stars">
              <i class="fa fa-star" v-for="n in 5" :key="n" :class="{ 'star-active': n <= editingOrder.star }"
                @click="editingOrder.star = n" />
            </div>
            <span class="rating-value">{{ editingOrder.star }}分</span>
          </div>

          <div class="comment-section">
            <label>评论内容：</label>
            <textarea v-model="editingOrder.comment" placeholder="请输入您的评论..." class="comment-textarea" rows="4"
              maxlength="200"></textarea>
            <div class="char-count">{{ editingOrder.comment ? editingOrder.comment.length : 0 }}/200</div>
          </div>
        </div>
        <div class="modal-footer">
          <button class="btn cancel-btn" @click="closeEditModal">取消</button>
          <button class="btn confirm-btn" @click="submitEdit">保存修改</button>
        </div>
      </div>
    </div>

    <!-- 底部导航 -->
    <footer class="bottom-nav">
      <div class="nav-item" :class="{ active: $route.path === '/index' }" @click="$router.push('/index')">
        <i class="fa fa-home"></i>
        <span>首页</span>
      </div>
      <div class="nav-item">
        <i class="fa fa-compass"></i>
        <span>商家</span>
      </div>
      <div class="nav-item" :class="{ active: $route.path === '/orderList' }" @click="$router.push('/orderList')">
        <i class="far fa-file-alt"></i>
        <span>订单</span>
      </div>
      <div class="nav-item" :class="{ active: $route.path === '/userProfile' }" @click="$router.push('/userProfile')">
        <i class="fa fa-user"></i>
        <span>我的</span>
      </div>
    </footer>
  </div>
</template>

<script>
export default {
  name: 'MyComments',
  data() {
    return {
      orderArr: [],
      userId: '',
      isLoading: true,
      user: {},
      token: '',
      showEditModal: false,
      editingOrder: {},
    };
  },
  created() {
    this.token = localStorage.getItem('token');
    this.$axios.get(`user`, {
      headers: {
        'Authorization': `Bearer ${this.token}`
      }
    }).then(response => {
      this.user = response.data.data;
      this.loadComments();
    }).catch(error => {
      console.error(error);
      this.isLoading = false;
    });
  },
  methods: {
    loadComments() {
      if (!this.user.id) {
        this.isLoading = false;
        this.orderArr = [];
        return;
      }

      this.isLoading = true;
      this.$axios
        .get(`orders/${this.user.id}/user`, {
          headers: { Authorization: `Bearer ${localStorage.getItem('token')}` }
        })
        .then(res => {
          const orders = res.data.data || [];
          // 过滤出有评论的订单
          this.orderArr = orders.filter(order => order.comment && order.comment.trim() !== '');
        })
        .catch(() => {
          this.$message.error('加载评论失败，请稍后重试');
        })
        .finally(() => (this.isLoading = false));
    },

    formatTime(t) {
      if (!t) return '未知时间';
      const d = new Date(t);
      if (isNaN(d.getTime())) return '未知时间';
      const pad = n => (n < 10 ? '0' + n : n);
      return `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())} ${pad(d.getHours())}:${pad(d.getMinutes())}`;
    },

    openEditModal(order) {
      this.editingOrder = order;
      this.showEditModal = true;
      document.body.style.overflow = 'hidden';
    },

    closeEditModal() {
      this.showEditModal = false;
      this.editingOrder = {};
      document.body.style.overflow = 'auto';
    },

    submitEdit() {
      if (!this.editingOrder.comment.trim()) {
        this.$message.warning('请输入评论内容');
        return;
      }

      this.$axios.post(`comments`, {
        "id": this.editingOrder.id,
        "comment": this.editingOrder.comment,
        "star": this.editingOrder.star
      }, {
        headers: { Authorization: `Bearer ${localStorage.getItem('token')}` }
      }).then(() => {
        this.$message.success('修改成功！');
        this.closeEditModal();
        this.loadComments();
      }).catch(() => {
        this.$message.error('修改失败，请稍后重试');
      });
    },

    deleteComment(order) {
      this.$confirm('确定删除这条评论吗？删除后不可恢复！', '确认删除', {
        confirmButtonText: '确定删除',
        cancelButtonText: '取消',
        type: 'warning',
        customClass: 'delete-confirm-modal'
      }).then(() => {
        this.$axios.post(`comments`, {
          "id": order.id,
          "comment": null,
          "star": order.star
        }, {
          headers: { Authorization: `Bearer ${localStorage.getItem('token')}` }
        }).then(() => {
          this.$message.success('删除成功！');
          this.loadComments();
        }).catch(() => {
          this.$message.error('删除失败，请稍后重试');
        });
      }).catch(() => {
        // 用户取消删除
      });
    }
  }
};
</script>

<style scoped>
.comments-container {
  min-height: 100vh;
  background: linear-gradient(135deg, #f5f7fa 0%, #e8f4f8 100%);
  font-family: 'PingFang SC', 'Helvetica Neue', Arial, sans-serif;
}

/* 顶部导航栏 */
.comments-header {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 12vw;
  background: linear-gradient(135deg, #0097FF 0%, #0066cc 100%);
  color: white;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 4vw;
  z-index: 1000;
  box-shadow: 0 4px 20px rgba(0, 151, 255, 0.3);
}

.back-btn {
  font-size: 5vw;
  cursor: pointer;
  transition: transform 0.3s ease;
}

.back-btn:hover {
  transform: translateX(-3px);
}

.comments-header h1 {
  font-size: 4.5vw;
  font-weight: 600;
  margin: 0;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.header-placeholder {
  width: 8vw;
}

/* 加载状态 */
.loading-container {
  padding-top: 25vw;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: #666;
}

.loading-spinner {
  margin-bottom: 3vw;
}

.loading-spinner i {
  font-size: 8vw;
  color: #0097FF;
  animation: spin 1s linear infinite;
}

.loading-container p {
  font-size: 3.8vw;
  margin: 0;
}

@keyframes spin {
  0% {
    transform: rotate(0deg);
  }

  100% {
    transform: rotate(360deg);
  }
}

/* 主要内容区域 */
.comments-main {
  padding: 16vw 4vw 20vw;
}

.comments-count {
  display: flex;
  align-items: center;
  background: white;
  padding: 3vw 4vw;
  border-radius: 12px;
  margin-bottom: 3vw;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
}

.comments-count i {
  color: #0097FF;
  font-size: 4.5vw;
  margin-right: 2vw;
}

.comments-count span {
  font-size: 3.8vw;
  color: #333;
  font-weight: 500;
}

/* 评论列表 */
.comments-list {
  list-style: none;
  padding: 0;
  margin: 0;
}

.comment-item {
  background: white;
  border-radius: 16px;
  padding: 4vw;
  margin-bottom: 3vw;
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
  transition: all 0.3s ease;
  border: 1px solid rgba(255, 255, 255, 0.2);
}

.comment-item:hover {
  transform: translateY(-3px);
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.15);
}

.comment-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 3vw;
}

.shop-info {
  display: flex;
  align-items: center;
  flex: 1;
}

.shop-image {
  width: 14vw;
  height: 14vw;
  border-radius: 10px;
  object-fit: cover;
  margin-right: 3vw;
  border: 2px solid #f0f0f0;
}

.shop-details {
  flex: 1;
}

.shop-title {
  font-size: 4.2vw;
  font-weight: 600;
  color: #333;
  margin: 0 0 1vw;
  line-height: 1.3;
}

.comment-date {
  font-size: 3.2vw;
  color: #888;
  margin: 0;
}

.rating-display {
  text-align: right;
}

.rating-text {
  display: block;
  font-size: 3.5vw;
  color: #FF9800;
  font-weight: 600;
  margin-bottom: 1vw;
}

.rating-stars {
  display: flex;
  gap: 0.5vw;
  font-size: 3.5vw;
}

.rating-stars .rated {
  color: #FFC107;
}

/* 评论内容 */
.comment-content {
  margin-bottom: 3vw;
}

.comment-text {
  font-size: 3.8vw;
  line-height: 1.6;
  color: #555;
  margin: 0;
  padding: 3vw;
  background: linear-gradient(135deg, #f8f9ff 0%, #f0f4ff 100%);
  border-radius: 10px;
  border-left: 4px solid #0097FF;
  position: relative;
}

.comment-text::before {
  content: '"';
  font-size: 6vw;
  color: #0097FF;
  position: absolute;
  top: 1vw;
  left: 2vw;
  opacity: 0.3;
}

/* 操作按钮 */
.comment-actions {
  display: flex;
  justify-content: flex-end;
  gap: 2vw;
  padding-top: 3vw;
  border-top: 1px solid #f0f0f0;
}

.action-btn {
  display: flex;
  align-items: center;
  gap: 1.5vw;
  padding: 2vw 3.5vw;
  border: none;
  border-radius: 20px;
  font-size: 3.4vw;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.3s ease;
  min-width: 20vw;
  justify-content: center;
}

.action-btn i {
  font-size: 3.8vw;
}

.modify-btn {
  background: linear-gradient(135deg, #E3F2FD 0%, #BBDEFB 100%);
  color: #1976D2;
}

.modify-btn:hover {
  background: linear-gradient(135deg, #BBDEFB 0%, #90CAF9 100%);
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(25, 118, 210, 0.3);
}

.remove-btn {
  background: linear-gradient(135deg, #FFEBEE 0%, #FFCDD2 100%);
  color: #D32F2F;
}

.remove-btn:hover {
  background: linear-gradient(135deg, #FFCDD2 0%, #EF9A9A 100%);
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(211, 47, 47, 0.3);
}

/* 空状态 */
.no-comments {
  padding: 25vw 4vw 20vw;
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 60vh;
}

.empty-state {
  text-align: center;
  max-width: 80vw;
}

.empty-icon {
  width: 25vw;
  height: 25vw;
  background: linear-gradient(135deg, #E3F2FD 0%, #BBDEFB 100%);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0 auto 4vw;
}

.empty-icon i {
  font-size: 10vw;
  color: #2196F3;
}

.empty-state h3 {
  font-size: 4.5vw;
  color: #333;
  margin: 0 0 2vw;
  font-weight: 600;
}

.empty-state p {
  font-size: 3.8vw;
  color: #666;
  margin: 0 0 6vw;
}

.browse-shops-btn {
  display: flex;
  align-items: center;
  gap: 2vw;
  padding: 3vw 8vw;
  background: linear-gradient(135deg, #0097FF 0%, #0066cc 100%);
  color: white;
  border: none;
  border-radius: 25px;
  font-size: 3.8vw;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.3s ease;
  box-shadow: 0 4px 15px rgba(0, 151, 255, 0.4);
  margin: 0 auto;
}

.browse-shops-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(0, 151, 255, 0.6);
}

.browse-shops-btn i {
  font-size: 4vw;
}

/* 编辑模态框 */
.edit-modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.6);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 2000;
  padding: 4vw;
  backdrop-filter: blur(5px);
}

.edit-modal-content {
  background: white;
  border-radius: 16px;
  width: 100%;
  max-width: 90vw;
  max-height: 85vh;
  overflow-y: auto;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.3);
  animation: modalSlideIn 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

@keyframes modalSlideIn {
  from {
    opacity: 0;
    transform: scale(0.9) translateY(-20px);
  }

  to {
    opacity: 1;
    transform: scale(1) translateY(0);
  }
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 4vw;
  border-bottom: 1px solid #f0f0f0;
  background: linear-gradient(135deg, #f8f9ff 0%, #f0f4ff 100%);
  border-radius: 16px 16px 0 0;
}

.modal-header h2 {
  margin: 0;
  color: #333;
  font-size: 4.5vw;
  font-weight: 600;
}

.close-modal-btn {
  background: none;
  border: none;
  font-size: 5vw;
  color: #666;
  cursor: pointer;
  padding: 1vw;
  border-radius: 50%;
  transition: background-color 0.3s ease;
  width: 8vw;
  height: 8vw;
  display: flex;
  align-items: center;
  justify-content: center;
}

.close-modal-btn:hover {
  background: #f0f0f0;
  color: #333;
}

.modal-body {
  padding: 4vw;
}

.modal-shop-info {
  display: flex;
  align-items: center;
  margin-bottom: 4vw;
  padding-bottom: 3vw;
  border-bottom: 1px solid #f0f0f0;
}

.modal-shop-image {
  width: 12vw;
  height: 12vw;
  object-fit: cover;
  border-radius: 8px;
  margin-right: 3vw;
  border: 2px solid #f0f0f0;
}

.modal-shop-details h3 {
  margin: 0 0 1vw;
  font-size: 4vw;
  color: #333;
  font-weight: 600;
}

.modal-shop-details p {
  margin: 0;
  font-size: 3.2vw;
  color: #888;
}

.rating-section {
  margin-bottom: 4vw;
}

.rating-section label {
  display: block;
  margin-bottom: 3vw;
  font-weight: 500;
  color: #333;
  font-size: 4vw;
}

.modal-rating-stars {
  display: flex;
  gap: 2vw;
  margin-bottom: 2vw;
}

.modal-rating-stars .fa-star {
  font-size: 6vw;
  color: #ddd;
  cursor: pointer;
  transition: all 0.2s ease;
}

.modal-rating-stars .star-active {
  color: #FFC107;
  transform: scale(1.1);
}

.modal-rating-stars .fa-star:hover {
  transform: scale(1.2);
}

.rating-value {
  font-size: 3.8vw;
  color: #FF9800;
  font-weight: 600;
}

.comment-section {
  margin-bottom: 2vw;
}

.comment-section label {
  display: block;
  margin-bottom: 3vw;
  font-weight: 500;
  color: #333;
  font-size: 4vw;
}

.comment-textarea {
  width: 100%;
  padding: 3vw;
  border: 2px solid #e0e0e0;
  border-radius: 10px;
  font-size: 3.8vw;
  font-family: inherit;
  resize: vertical;
  min-height: 25vw;
  box-sizing: border-box;
  transition: border-color 0.3s ease;
  background: #fafafa;
}

.comment-textarea:focus {
  outline: none;
  border-color: #0097FF;
  background: white;
  box-shadow: 0 0 0 3px rgba(0, 151, 255, 0.1);
}

.char-count {
  text-align: right;
  font-size: 3.2vw;
  color: #888;
  margin-top: 1vw;
}

.modal-footer {
  display: flex;
  justify-content: flex-end;
  gap: 2vw;
  padding: 4vw;
  border-top: 1px solid #f0f0f0;
  background: #fafafa;
  border-radius: 0 0 16px 16px;
}

.btn {
  padding: 2.5vw 5vw;
  border: none;
  border-radius: 20px;
  font-size: 3.8vw;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.3s ease;
  min-width: 20vw;
}

.cancel-btn {
  background: #f0f0f0;
  color: #666;
}

.cancel-btn:hover {
  background: #e0e0e0;
  transform: translateY(-1px);
}

.confirm-btn {
  background: linear-gradient(135deg, #0097FF 0%, #0066cc 100%);
  color: white;
  box-shadow: 0 4px 12px rgba(0, 151, 255, 0.4);
}

.confirm-btn:hover {
  transform: translateY(-1px);
  box-shadow: 0 6px 16px rgba(0, 151, 255, 0.6);
}

/* 底部导航 */
.bottom-nav {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 16vw;
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  border-top: 1px solid #e0e0e0;
  display: flex;
  justify-content: space-around;
  align-items: center;
  z-index: 100;
}

.nav-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  color: #999;
  font-size: 3vw;
  cursor: pointer;
  transition: all 0.3s ease;
  padding: 2vw;
  border-radius: 10px;
}

.nav-item.active {
  color: #0097FF;
  transform: translateY(-3px);
}

.nav-item i {
  font-size: 5.5vw;
  margin-bottom: 1vw;
  transition: all 0.3s ease;
}

.nav-item.active i {
  transform: scale(1.2);
}

.nav-item span {
  font-size: 3vw;
  font-weight: 500;
}

/* 响应式设计 */
@media (min-width: 768px) {
  .comments-container {
    max-width: 500px;
    margin: 0 auto;
    box-shadow: 0 0 20px rgba(0, 0, 0, 0.1);
  }

  .comments-header {
    height: 60px;
    padding: 0 20px;
  }

  .comments-header h1 {
    font-size: 20px;
  }

  .back-btn {
    font-size: 20px;
  }

  /* 其他桌面端样式调整... */
}
</style>